//
//  SQApplyForViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/14.
//

#import "SQApplyForViewController.h"

#import "SQHomeworkLinkViewController.h"

#import "SQTaskApplyPlaceModel.h"
#import "SQApplyForModel.h"
#import "SQHomeworkLinkModel.h"

#import "PickerSelectView.h"
#import "HRBDateSelectView.h"
#import "CrazyFunction.h"


@interface SQApplyForViewController ()

@property (strong, nonatomic) SQTaskApplyPlaceModel *taskApplyPlaceModel;

@property (weak, nonatomic) IBOutlet UITextField *tf_area;//区域
@property (weak, nonatomic) IBOutlet UITextField *tf_task;//工作任务
@property (weak, nonatomic) IBOutlet UITextField *tf_homeworkLink;//作业环节
@property (weak, nonatomic) IBOutlet UITextField *tf_startTime;//开始时间
@property (weak, nonatomic) IBOutlet UITextField *tf_endTime;//结束时间

@property (copy, nonatomic) NSString *sa_site_area;
@property (copy, nonatomic) NSString *sa_work_assignment;
@property (copy, nonatomic) NSString *sa_task_release;
@property (copy, nonatomic) NSString *sa_task_release_text;
@property (copy, nonatomic) NSString *sa_operation_link;
@property (copy, nonatomic) NSString *sa_start;
@property (copy, nonatomic) NSString *sa_end;
@property (copy, nonatomic) NSString *sa_enterprise;

@property (copy, nonatomic) NSString * ipm_unit_two;
@property (copy, nonatomic) NSString * ipm_unit_three;

@property ( strong ,nonatomic) NSArray *accidentTypesArray;

@end

@implementation SQApplyForViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"作业申请";
    [self getTaskApplyPlaceData];
    
}

-(void)getTaskApplyPlaceData{
    [SQTaskApplyPlaceModel getDataWithParams:@{@"sa_enterprise":MEnterpriseId} WithCallBack:^(SQTaskApplyPlaceModel * data) {
        self.taskApplyPlaceModel = data;
        
    }];
}


//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

/**区域*/
- (IBAction)clickArea:(UIButton *)sender {
    if (![self.tf_task.text isEqualToString:@""]) {
        self.tf_task.text = @"";
        self.tf_homeworkLink.text = @"";
        self.sa_operation_link = @"";
    }
    if (self.taskApplyPlaceModel.nameArr.count == 0) {return;}
    
    self.taskApplyPlaceModel.nameArr.pickerSigleShow(self.tf_area,@"场所区域");
}

/**工作任务*/
- (IBAction)clickTask:(UIButton *)sender {
    
    if ([self.tf_area.text isEqualToString:@""]) {
        [CCToast showMessage:@"请选择场所区域"];
        return;
    }
    
    if (self.taskApplyPlaceModel.data[self.tf_area.selectIndex[0].intValue].nameArr.count == 0) {return;}
    
    self.taskApplyPlaceModel.data[self.tf_area.selectIndex[0].intValue].nameArr.pickerSigleShow(self.tf_task,@"工作任务").finish = ^(UITextField * textField){
        SQTaskApplyPlaceModelData * data = self.taskApplyPlaceModel.data[self.tf_area.selectIndex[0].intValue];
        
        SQTaskApplyPlaceModelArray * modelArray = data.ipm_unit_three[self.tf_task.selectIndex[0].intValue];
        
        self.ipm_unit_two = data.ipm_unit_two;
        self.ipm_unit_three = modelArray.name;
       
        [self getHomeworkLinkData];
    };
    
    
}



/**作业环节*/
- (IBAction)clickHomeworkLink:(UIButton *)sender {
    if ([self.tf_task.text isEqualToString:@""]) {
        [CCToast showMessage:@"请选择工作任务"];
        return;
    }
    

    SQHomeworkLinkViewController * homeworkLinkVC = SQHomeworkLinkViewController.new;
    
    homeworkLinkVC.ipm_unit_two = self.ipm_unit_two;
    homeworkLinkVC.ipm_unit_three = self.ipm_unit_three;
    if (self.accidentTypesArray.count != 0) {
        homeworkLinkVC.accidentTypeIDs = self.accidentTypesArray;
        SQ_LOG(@"dddddddcc    %@",self.accidentTypesArray);
    }
    homeworkLinkVC.homeworkLinkBack = ^(id  _Nonnull str, id  _Nonnull josn,id  _Nonnull accidentTypesID) {
        
        self.tf_homeworkLink.text = str;
        self.sa_operation_link = josn;
        self.accidentTypesArray = accidentTypesID;
    };
    [self pushVC:homeworkLinkVC];
}


-(void)getHomeworkLinkData{
    NSMutableArray * arrayM = NSMutableArray.new;
    NSMutableArray * arrayMTitle = NSMutableArray.new;
 
    [SQHomeworkLinkModel getDataWithParams:@{@"ipm_unit_two":self.ipm_unit_two,@"ipm_unit_three":self.ipm_unit_three,@"sa_enterprise":MEnterpriseId} WithCallBack:^(SQHomeworkLinkModel * data) {
        
        for (SQHomeworkLinkModelData * modelData in data.data) {
            
            NSDictionary * dic = @{@"sal_operation_link_id":modelData.ipm_id,@"sal_operation_link_text":modelData.ipm_name};
            
            [arrayM addObject:dic];
            [arrayMTitle addObject:modelData.ipm_name];
        }
        
        NSString * josn = [CrazyFunction crazy_ObjectToJsonString:arrayM];

        
        self.sa_operation_link = josn;
        
        
    }];
}



/**开始时间*/
- (IBAction)clickStartTime:(UIButton *)sender {
    HRBDateSelectView.showForType(HRBDateSelectTypeAfter_YMD).formatString(HRBTimeFormat_YMD).result = ^(NSString * _Nonnull time, NSDate * _Nonnull date) {
        
        self.tf_startTime.text = time;
    };
    
}

/**结束时间*/
- (IBAction)clickEndTime:(UIButton *)sender {
    HRBDateSelectView.showForType(HRBDateSelectTypeAfter_YMD).formatString(HRBTimeFormat_YMD).result = ^(NSString * _Nonnull time, NSDate * _Nonnull date) {
        
        self.tf_endTime.text = time;
    };
}

- (IBAction)clickConfirm:(UIButton *)sender {
    
    if ([self verification]) {
    
        [SQApplyForModel getDataWithParams:@{@"sa_site_area":self.ipm_unit_two,
                                             @"sa_work_assignment":self.ipm_unit_three,
                                             @"sa_task_release":UID,
                                             @"sa_task_release_text":MName,
                                             @"sa_operation_link":self.sa_operation_link,
                                             @"sa_start":self.tf_startTime.text,
                                             @"sa_end":self.tf_endTime.text,
                                             @"sa_enterprise":MEnterpriseId
            
        } WithCallBack:^(SQApplyForModel * data) {
            [CCToast showMessage:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        
    }
    
    
}

#pragma mark ---- 验证
- (BOOL)verification {
    BOOL ret = NO;
    if([self.tf_area.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入所在地区"];
        return NO;
    }
    
    
    if([self.tf_task.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择工作任务"];
        return NO;
    }
   
    if([self.tf_startTime.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择作业开始时间"];
        return NO;
    }
    
    if([self.tf_endTime.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择作业结束时间"];
        return NO;
    }else{
        ret = YES;
    }
    return YES;
}


@end
